<template><div><h2 id="快速入门" tabindex="-1"><a class="header-anchor" href="#快速入门"><span>快速入门</span></a></h2>
<p>Yii 提供了一整套工具来简化实现 RESTful Web 服务 API 的任务。尤其是，Yii 支持以下关于 RESTful API 的特性:</p>
<ul>
<li>支持对 Active Record 的通用 API 的快速原型设计；</li>
<li>响应格式的约定（默认支持 JSON 和 XML）；</li>
<li>对可选择输出的字段，提供可自定义的对象序列化支持；</li>
<li>合理的格式化采集数据和验证错误；</li>
<li>数据集合的分页、过滤与排序；</li>
<li>支持 <a href="https://en.wikipedia.org/wiki/HATEOAS" target="_blank" rel="noopener noreferrer">HATEOAS</a>；</li>
<li>高效路由，并自带合理的 HTTP 动词检查；</li>
<li>内置对 OPTIONS 和 HEAD 动词的支持；</li>
<li>用户认证与授权；</li>
<li>数据缓存和 HTTP 缓存；</li>
<li>速率限制，即接口限流</li>
</ul>
<p>下面，我们用一个例子来说明，如何用最少的代码工作量来构建一组 RESTful API。<br>
假设，您希望通过 RESTful API 来展示用户数据。该用户数据存储在 <code v-pre>user</code> 数据库表中，并且您已经创建了活动记录类 <code v-pre>app\models\User</code> 来访问用户数据。</p>
<h2 id="创建控制器" tabindex="-1"><a class="header-anchor" href="#创建控制器"><span>创建控制器</span></a></h2>
<p>首先，创建一个控制器类 <code v-pre>app\controllers\UserController</code>，代码如下：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">namespace</span> <span class="token package">app<span class="token punctuation">\</span>controllers</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>rest<span class="token punctuation">\</span>ActiveController</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">UserController</span> <span class="token keyword">extends</span> <span class="token class-name">ActiveController</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token variable">$modelClass</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'app\models\User'</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>控制器类继承自 <code v-pre>yii\rest\ActiveController</code>，它实现了一组公共的 RESTful 操作。通过指定 <code v-pre>yii\rest\ActiveController::modelClass</code> 为 <code v-pre>app\models\User</code>，控制器就知道哪个模型可以用来获取和操作数据。</p>
<h2 id="配置-url-规则" tabindex="-1"><a class="header-anchor" href="#配置-url-规则"><span>配置 URL 规则</span></a></h2>
<p>然后，在你的应用程序配置中修改 <code v-pre>urlManager</code> 组件的配置：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'urlManager'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'enablePrettyUrl'</span> <span class="token operator">=></span> <span class="token constant boolean">true</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'enableStrictParsing'</span> <span class="token operator">=></span> <span class="token constant boolean">true</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'showScriptName'</span> <span class="token operator">=></span> <span class="token constant boolean">false</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'rules'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'yii\rest\UrlRule'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'controller'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'user'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>上面的配置主要为 <code v-pre>user</code> 控制器添加了一个 URL 规则，这样就可以使用美化的 URL 和有意义的 HTTP 动词访问和操作用户数据。</p>
<blockquote>
<p>注意：Yii 将自动以复数形式将控制器名称用于端点（参考下面的 <a href="#f421d7">小试牛刀</a> 章节），你可以使用<code v-pre>yii\rest\UrlRule::$pluralize</code> 属性来配置它。</p>
</blockquote>
<h2 id="支持-json-输入" tabindex="-1"><a class="header-anchor" href="#支持-json-输入"><span>支持 JSON 输入</span></a></h2>
<p>要让 API 接受 JSON 格式的输入数据，需要配置 <code v-pre>request</code> 应用组件的 <code v-pre>yii\web\Request::$parser</code> 属性来使用这个针对 JSON 输入的 <code v-pre>yii\web\JsonParser</code>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'request'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'parsers'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'application/json'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'yii\web\JsonParser'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span></span>
<span class="line"><span class="token punctuation">]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>注意：以上配置是可选的，如果没有上面的配置，API 将只能识别 <code v-pre>application/x-www-form-urlencoded</code> 和 <code v-pre>multipart/form-data</code> 输入格式。</p>
</blockquote>
<h2 id="小试牛刀" tabindex="-1"><a class="header-anchor" href="#小试牛刀"><span>小试牛刀</span></a></h2>
<p>通过上述最简单的工作，您已经完成了创建用于访问用户数据的 RESTful API 的任务。您创建的 API 包括：</p>
<ul>
<li><code v-pre>GET /users</code>：逐页列出所有用户；</li>
<li><code v-pre>HEAD /users</code>：显示用户列表的概览信息；</li>
<li><code v-pre>POST /users</code>：创建一个新用户；</li>
<li><code v-pre>GET /users/123</code>：返回用户 123 的详细信息；</li>
<li><code v-pre>HEAD /users/123</code>：显示用户 123 的概览信息；</li>
<li><code v-pre>PATCH /users/123</code> 和 <code v-pre>PUT /users/123</code>：更新用户 123；</li>
<li><code v-pre>DELETE /users/123</code>：删除用户 123；</li>
<li><code v-pre>OPTIONS /users</code>：显示关于端点 <code v-pre>/users</code> 所支持的动词；</li>
<li><code v-pre>OPTIONS /users/123</code>：显示关于端点 <code v-pre>/users/123</code> 所支持的动词。</li>
</ul>
<p>你可以使用 <code v-pre>curl</code> 命令访问 API，具体如下：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">$ <span class="token function">curl</span> <span class="token parameter variable">-i</span> <span class="token parameter variable">-H</span> <span class="token string">"Accept:application/json"</span> <span class="token string">"http://localhost/users"</span></span>
<span class="line"></span>
<span class="line">HTTP/1.1 <span class="token number">200</span> OK</span>
<span class="line"><span class="token punctuation">..</span>.</span>
<span class="line">X-Pagination-Total-Count: <span class="token number">1000</span></span>
<span class="line">X-Pagination-Page-Count: <span class="token number">50</span></span>
<span class="line">X-Pagination-Current-Page: <span class="token number">1</span></span>
<span class="line">X-Pagination-Per-Page: <span class="token number">20</span></span>
<span class="line">Link: <span class="token operator">&lt;</span>http://localhost/users?page<span class="token operator">=</span><span class="token operator"><span class="token file-descriptor important">1</span>></span><span class="token punctuation">;</span> <span class="token assign-left variable">rel</span><span class="token operator">=</span>self,</span>
<span class="line">      <span class="token operator">&lt;</span>http://localhost/users?page<span class="token operator">=</span><span class="token operator"><span class="token file-descriptor important">2</span>></span><span class="token punctuation">;</span> <span class="token assign-left variable">rel</span><span class="token operator">=</span>next,</span>
<span class="line">      <span class="token operator">&lt;</span>http://localhost/users?page<span class="token operator">=</span><span class="token number">5</span><span class="token operator"><span class="token file-descriptor important">0</span>></span><span class="token punctuation">;</span> <span class="token assign-left variable">rel</span><span class="token operator">=</span>last</span>
<span class="line">Transfer-Encoding: chunked</span>
<span class="line">Content-Type: application/json<span class="token punctuation">;</span> <span class="token assign-left variable">charset</span><span class="token operator">=</span>UTF-8</span>
<span class="line"></span>
<span class="line"><span class="token punctuation">[</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token string">"id"</span><span class="token builtin class-name">:</span> <span class="token number">1</span>,</span>
<span class="line">        <span class="token punctuation">..</span>.</span>
<span class="line">    <span class="token punctuation">}</span>,</span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token string">"id"</span><span class="token builtin class-name">:</span> <span class="token number">2</span>,</span>
<span class="line">        <span class="token punctuation">..</span>.</span>
<span class="line">    <span class="token punctuation">}</span>,</span>
<span class="line">    <span class="token punctuation">..</span>.</span>
<span class="line"><span class="token punctuation">]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>尝试将可接受的内容类型（content type）更改为 <code v-pre>application/xml</code>，您将看到结果以 XML 格式返回：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">$ <span class="token function">curl</span> <span class="token parameter variable">-i</span> <span class="token parameter variable">-H</span> <span class="token string">"Accept:application/xml"</span> <span class="token string">"http://localhost/users"</span></span>
<span class="line"></span>
<span class="line">HTTP/1.1 <span class="token number">200</span> OK</span>
<span class="line"><span class="token punctuation">..</span>.</span>
<span class="line">X-Pagination-Total-Count: <span class="token number">1000</span></span>
<span class="line">X-Pagination-Page-Count: <span class="token number">50</span></span>
<span class="line">X-Pagination-Current-Page: <span class="token number">1</span></span>
<span class="line">X-Pagination-Per-Page: <span class="token number">20</span></span>
<span class="line">Link: <span class="token operator">&lt;</span>http://localhost/users?page<span class="token operator">=</span><span class="token operator"><span class="token file-descriptor important">1</span>></span><span class="token punctuation">;</span> <span class="token assign-left variable">rel</span><span class="token operator">=</span>self, </span>
<span class="line">      <span class="token operator">&lt;</span>http://localhost/users?page<span class="token operator">=</span><span class="token operator"><span class="token file-descriptor important">2</span>></span><span class="token punctuation">;</span> <span class="token assign-left variable">rel</span><span class="token operator">=</span>next, </span>
<span class="line">      <span class="token operator">&lt;</span>http://localhost/users?page<span class="token operator">=</span><span class="token number">5</span><span class="token operator"><span class="token file-descriptor important">0</span>></span><span class="token punctuation">;</span> <span class="token assign-left variable">rel</span><span class="token operator">=</span>last</span>
<span class="line">Transfer-Encoding: chunked</span>
<span class="line">Content-Type: application/xml</span>
<span class="line"></span>
<span class="line"><span class="token operator">&lt;</span>?xml <span class="token assign-left variable">version</span><span class="token operator">=</span><span class="token string">"1.0"</span> <span class="token assign-left variable">encoding</span><span class="token operator">=</span><span class="token string">"UTF-8"</span>?<span class="token operator">></span></span>
<span class="line"><span class="token operator">&lt;</span>response<span class="token operator">></span></span>
<span class="line">    <span class="token operator">&lt;</span>item<span class="token operator">></span></span>
<span class="line">        <span class="token operator">&lt;</span>id<span class="token operator">></span><span class="token operator"><span class="token file-descriptor important">1</span>&lt;</span>/id<span class="token operator">></span></span>
<span class="line">        <span class="token punctuation">..</span>.</span>
<span class="line">    <span class="token operator">&lt;</span>/item<span class="token operator">></span></span>
<span class="line">    <span class="token operator">&lt;</span>item<span class="token operator">></span></span>
<span class="line">        <span class="token operator">&lt;</span>id<span class="token operator">></span><span class="token operator"><span class="token file-descriptor important">2</span>&lt;</span>/id<span class="token operator">></span></span>
<span class="line">        <span class="token punctuation">..</span>.</span>
<span class="line">    <span class="token operator">&lt;</span>/item<span class="token operator">></span></span>
<span class="line">    <span class="token punctuation">..</span>.</span>
<span class="line"><span class="token operator">&lt;</span>/response<span class="token operator">></span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>下面的命令将通过以 JSON 格式发送带有用户数据的 POST 请求来创建一个新用户：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">$ <span class="token function">curl</span> <span class="token parameter variable">-i</span> <span class="token parameter variable">-H</span> <span class="token string">"Accept:application/json"</span> <span class="token parameter variable">-H</span> <span class="token string">"Content-Type:application/json"</span></span>
<span class="line">    <span class="token parameter variable">-XPOST</span> <span class="token string">"http://localhost/users"</span></span>
<span class="line">    <span class="token parameter variable">-d</span> <span class="token string">'{"username": "example", "email": "user@example.com"}'</span></span>
<span class="line"></span>
<span class="line">HTTP/1.1 <span class="token number">201</span> Created</span>
<span class="line"><span class="token punctuation">..</span>.</span>
<span class="line">Location: http://localhost/users/1</span>
<span class="line">Content-Length: <span class="token number">99</span></span>
<span class="line">Content-Type: application/json<span class="token punctuation">;</span> <span class="token assign-left variable">charset</span><span class="token operator">=</span>UTF-8</span>
<span class="line"></span>
<span class="line"><span class="token punctuation">{</span><span class="token string">"id"</span>:1,<span class="token string">"username"</span><span class="token builtin class-name">:</span><span class="token string">"example"</span>,<span class="token string">"email"</span><span class="token builtin class-name">:</span><span class="token string">"user@example.com"</span>,<span class="token string">"created_at"</span>:1414674789,<span class="token string">"updated_at"</span>:1414674789<span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>Tip：您也可以通过输入URL <code v-pre>http://localhost/users</code> 通过Web浏览器访问api。然而，您可能需要一些浏览器插件来发送特定的请求头。</p>
</blockquote>
<p>如您所见，在响应头中，有关于总计数、页面计数等信息，也有一些链接允许您导航到其他数据页面。例如，<code v-pre>http://localhost/users?page=2</code> 会给出用户数据的下一页。</p>
<p>使用 <code v-pre>fields</code> 和 <code v-pre>expand</code> 参数，您还可以指定结果中应该包含哪些字段。例如，URL <code v-pre>http://localhost/users?fields=id,email</code> 将只返回 <code v-pre>id</code> 和 <code v-pre>email</code> 字段。</p>
<blockquote>
<p>Info：您可能已经注意到 <code v-pre>http://localhost/users</code> 的结果包含一些敏感字段，如 <code v-pre>password_hash</code>，<code v-pre>auth_key</code>。您当然不希望这些出现在 API 结果中。您可以并且应该从结果中删除这些字段，如参考资源（Resources）章节所述。</p>
</blockquote>
<p>此外，您还可以对数据集合进行排序，如 <code v-pre>http://localhost/users?sort=email</code> 或 <code v-pre>http://localhost/users?sort=-email</code>，也可以对数据集合进行过滤，如 <code v-pre>http://localhost/users?filter[id]=10</code> 或 <code v-pre>http://localhost/users?filter[email][like]=gmail.com</code> 可以通过使用数据过滤器来实现。有关详细信息，请参阅资源（Resources）章节。</p>
<h2 id="自定义列表分页和排序" tabindex="-1"><a class="header-anchor" href="#自定义列表分页和排序"><span>自定义列表分页和排序</span></a></h2>
<p>为了改变默认的分页和排序模型列表，你可以在你的控制器中配置 <code v-pre>yii\rest\IndexAction</code>。例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">namespace</span> <span class="token package">app<span class="token punctuation">\</span>controllers</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>rest<span class="token punctuation">\</span>ActiveController</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">yii<span class="token punctuation">\</span>helpers<span class="token punctuation">\</span>ArrayHelper</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">UserController</span> <span class="token keyword">extends</span> <span class="token class-name">ActiveController</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token variable">$modelClass</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'app\models\User'</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">actions</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token class-name static-context">ArrayHelper</span><span class="token operator">::</span><span class="token function">merge</span><span class="token punctuation">(</span><span class="token keyword static-context">parent</span><span class="token operator">::</span><span class="token function">actions</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'index'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">                <span class="token string single-quoted-string">'pagination'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">                    <span class="token string single-quoted-string">'pageSize'</span> <span class="token operator">=></span> <span class="token number">10</span><span class="token punctuation">,</span></span>
<span class="line">                <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">                <span class="token string single-quoted-string">'sort'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">                    <span class="token string single-quoted-string">'defaultOrder'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">                        <span class="token string single-quoted-string">'created_at'</span> <span class="token operator">=></span> <span class="token constant">SORT_DESC</span><span class="token punctuation">,</span></span>
<span class="line">                    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">                <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>请查看 <a href="#">扩展 ActiveController</a> 来获取配置 ActiveController 的动作（actions）的详细信息。</p>
<h2 id="小结" tabindex="-1"><a class="header-anchor" href="#小结"><span>小结</span></a></h2>
<p>使用 Yii RESTful API 框架，可以根据控制器操作实现 API 端点（endpoint），并且可以使用控制器组织动作（action），这些动作为单一类型的资源实现端点。</p>
<p>资源（Resources）被表示为 <code v-pre>yii\base\Model</code> 类扩展的数据模型。如果您正在使用数据库（关系型或 NoSQL），建议您使用 <code v-pre>yii\db\ActiveRecord</code> 来表示资源。</p>
<p>你可以使用 <code v-pre>yii\rest\UrlRule</code> 来简化到你的 API 端点的路由。</p>
<p>虽然不是必需的，但建议您将 RESTful API 开发为独立的应用程序（如，命名为 api），与 Web 前端（frontend）和后端（backend）不同，以便更容易维护。</p>
<blockquote>
<p>💖喜欢本文档的，欢迎点赞、收藏、留言或转发，谢谢支持！<br>
作者邮箱：zhuzixian520@126.com，github地址：<a href="https://github.com/zhuzixian520" target="_blank" rel="noopener noreferrer">github.com/zhuzixian520</a></p>
</blockquote>
</div></template>


